package me.luger.core.commom.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Title KwCookieUtil.java
 * @Description cookie操作类
 * @Copyright 2016 © kuwo.cn
 * @author TianHaoyuan
 * @Created 2016/01/15
 * @Version: 1.0
 */
public class CookieUtil {

	/**
	 * 域名列表
	 */
	private static List<String> domains = null;

	/**
	 * 默认cookie时间
	 */
	private static int defaultMaxAge = 3600;

	private static final String ROOT_MARK = "/";

	/**
	 * 设置cookie
	 * 
	 * @param response
	 * @param domain
	 * @param path
	 * @param name
	 * @param value
	 */
	public static void setCookie(HttpServletResponse response, String domain,
								 String path, String name, String value) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(defaultMaxAge);
		if (path != null && !path.isEmpty()) {
			cookie.setPath(path);
		}
		if (domain != null && !domain.isEmpty()) {
			cookie.setDomain(domain);
		}
		response.addCookie(cookie);
	}

	/**
	 * 设置cookie
	 * 
	 * @param response
	 * @param domain
	 * @param time
	 * @param path
	 * @param name
	 * @param value
	 */
	public static void setCookie(HttpServletResponse response, String domain,
								 String path, int time, String name, String value) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(time);
		if (path != null && !path.isEmpty()) {
			cookie.setPath(path);
		}
		if (domain != null && !domain.isEmpty()) {
			cookie.setDomain(domain);
		}
		response.addCookie(cookie);
	}

	/**
	 * 设置cookie
	 * 
	 * @param response
	 * @param path
	 * @param name
	 * @param value
	 */
	public static void setCookie(HttpServletResponse response, String path,
								 String name, String value) {
			setCookie(response, null, path, defaultMaxAge, name, value);
	}
	
	/**
	 * 设置cookie
	 * 
	 * @param response
	 * @param path
	 * @param name
	 * @param value
	 */
	public static void setCookieSyncAllDomains(HttpServletResponse response, String path,
											   String name, String value) {
		if (domains != null && !domains.isEmpty()) {
			for (String domain : domains) {
				setCookie(response, domain, path, defaultMaxAge, name, value);
			}
		} else {
			setCookie(response, null, path, defaultMaxAge, name, value);
		}
	}

	/**
	 * 设置cookie
	 * 
	 * @param response
	 * @param time
	 * @param path
	 * @param name
	 * @param value
	 */
	public static void setCookie(HttpServletResponse response, int time,
								 String path, String name, String value) {
			setCookie(response, null, path, time, name, value);
	}
	
	/**
	 * 设置cookie
	 * 
	 * @param response
	 * @param time
	 * @param path
	 * @param name
	 * @param value
	 */
	public static void setCookieSyncAllDomains(HttpServletResponse response, int time,
											   String path, String name, String value) {
		if (domains != null && !domains.isEmpty()) {
			for (String domain : domains) {
				setCookie(response, domain, path, time, name, value);
			}
		} else {
			setCookie(response, null, path, time, name, value);
		}
	}

	public static void setRootCookie(HttpServletResponse response, int time,
									 String name, String value) {
		setCookie(response, null, ROOT_MARK, time, name, value);
	}
	
	public static void setRootCookieSyncAllDomains(HttpServletResponse response, int time,
												   String name, String value) {
		if (domains != null && !domains.isEmpty()) {
			for (String domain : domains) {
				setCookie(response, domain, ROOT_MARK, time, name, value);
			}
		} else {
			setCookie(response, null, ROOT_MARK, time, name, value);
		}
	}

	public static void setLoaclCookie(HttpServletResponse response, int time,
									  String name, String value) {
		setCookie(response, null, null, time, name, value);
	}
	
	public static void setLoaclCookieSyncAllDomains(HttpServletResponse response, int time,
													String name, String value) {
		if (domains != null && !domains.isEmpty()) {
			for (String domain : domains) {
				setCookie(response, domain, null, time, name, value);
			}
		} else {
			setCookie(response, null, null, time, name, value);
		}
	}

	public static void setAnalysisCookie(HttpServletResponse response,
			int time, String name, String value) {
		setCookie(response, null, "/analysis/", time, name, value);
	}
	
	public static void setAnalysisCookieSyncAllDomains(HttpServletResponse response,
			int time, String name, String value) {
		if (domains != null && !domains.isEmpty()) {
			for (String domain : domains) {
				setCookie(response, domain, "/analysis/", time, name, value);
			}
		} else {
			setCookie(response, null, "/analysis/", time, name, value);
		}
	}

	/**
	 * 批量获取cookie
	 * 
	 * @param request
	 * @return
	 */
	public static Map<String, String> getCookies(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			return null;
		}
		Map<String, String> result = new HashMap<String, String>();
		for (Cookie cookie : cookies) {
			result.put(cookie.getName(), cookie.getValue());
		}
		if (result.isEmpty()) {
			result = null;
		}
		return result;
	}

	/**
	 * 批量获取cookie
	 * 
	 * @param request
	 * @param names
	 * @return
	 */
	public static Map<String, String> getCookies(HttpServletRequest request,
			Set<String> names) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			return null;
		}
		Map<String, String> result = new HashMap<String, String>();
		for (Cookie cookie : cookies) {
			String name = cookie.getName();
			if (names.contains(name)) {
				result.put(name, cookie.getValue());
			}
		}
		if (result.isEmpty()) {
			result = null;
		}
		return result;
	}

	/**
	 * 获取cookie
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static String getCookie(HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			return null;
		}

		for (Cookie cookie : cookies) {
			if (name.equals(cookie.getName())) {
				return cookie.getValue();
			}
		}

		return null;
	}

	/**
	 * 清空cookie
	 * 
	 * @param response
	 */
	public static void clearCookie(HttpServletResponse response, String path,
								   String name) {
		setCookie(response, null, path, 0, name, null);
	}
	
	/**
	 * 清空cookie
	 * 
	 * @param response
	 */
	public static void clearCookieSyncAllDomains(HttpServletResponse response, String path,
												 String name) {
		if (domains == null || domains.isEmpty()) {
			setCookie(response, null, path, 0, name, null);
		} else {
			for (String domain : domains) {
				// 清楚用户登录授权
				setCookie(response, domain, path, 0, name, null);
			}
		}
	}

	public static void clearRootCookie(HttpServletResponse response, String name) {
		setCookie(response, null, ROOT_MARK, 0, name, null);
	}

	public static void clearRootCookieSyncAllDomains(HttpServletResponse response, String name) {
		if (domains == null || domains.isEmpty()) {
			setCookie(response, null, ROOT_MARK, 0, name, null);
		} else {
			for (String domain : domains) {
				// 清楚用户登录授权
				setCookie(response, domain, ROOT_MARK, 0, name, null);
			}
		}
	}

	public static void clearLoaclCookie(HttpServletResponse response,
			String name) {
		setCookie(response, null, null, 0, name, null);
	}

	public static void clearLoaclCookieSyncAllDomains(HttpServletResponse response,
			String name) {
		if (domains == null || domains.isEmpty()) {
			setCookie(response, null, null, 0, name, null);
		} else {
			for (String domain : domains) {
				// 清楚用户登录授权
				setCookie(response, domain, null, 0, name, null);
			}
		}
	}

	/**
	 * 清空cookie
	 * 
	 * @param response
	 * @param domain
	 * @param path
	 * @param name
	 */
	public static void clearCookie(HttpServletResponse response, String domain,
								   String path, String name) {
		setCookie(response, domain, path, 0, name, null);
	}

	public List<String> getDomains() {
		return domains;
	}

	public void setDomains(List<String> domains) {
		this.domains = domains;
	}

	public int getDefaultMaxAge() {
		return defaultMaxAge;
	}

	public void setDefaultMaxAge(int defaultMaxAge) {
		this.defaultMaxAge = defaultMaxAge;
	}

}
